﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using HRM.Model;
using System.Windows;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Data;
using Microsoft.Practices.ServiceLocation;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Messaging;
using HRM.Helper;

namespace HRM.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        private readonly IFunction _functionService;

        public static NguoiSuDung User { get; set; }

        public List<ChucNang> Functions { get; private set; }

        public ProjectViewModel ProjectViewModel
        {
            get { return ServiceLocator.Current.GetInstance<ProjectViewModel>(); }
        }

        public FunctionViewModel FunctionViewModel
        {
            get { return ServiceLocator.Current.GetInstance<FunctionViewModel>(); }
        }

        public DepartmentViewModel DepartmentViewModel
        {
            get { return ServiceLocator.Current.GetInstance<DepartmentViewModel>(); }
        }

        public EducationViewModel EducationViewModel
        {
            get { return ServiceLocator.Current.GetInstance<EducationViewModel>(); }
        }

        public RegencyViewModel RegencyViewModel
        {
            get { return ServiceLocator.Current.GetInstance<RegencyViewModel>(); }
        }

        public CategoryCertificateViewModel CategoryCertificateViewModel
        {
            get { return ServiceLocator.Current.GetInstance<CategoryCertificateViewModel>(); }
        }

        /*public PieceSalaryBonusViewModel PieceSalaryBonusViewModel
        {
            //get { return ServiceLocator.Current.GetInstance<PieceSalaryBonusViewModel>(); }
        }*/

        public GroupUserViewModel GroupUserViewModel
        {
            get { return ServiceLocator.Current.GetInstance<GroupUserViewModel>(); }
        }

        public UserViewModel UserViewModel
        {
            get { return ServiceLocator.Current.GetInstance<UserViewModel>(); }
        }

        public StaffViewModel StaffViewModel
        {
            get { return ServiceLocator.Current.GetInstance<StaffViewModel>(); }
        }

        private ICollectionView _view;

        public ICollectionView FunctionsView
        {
            get
            {
                return _view;
            }
            set
            {
                if (_view != value)
                {
                    _view = value;
                    RaisePropertyChanged("FunctionsView");
                }
            }
        }

        private ChucNang _selectedFunction;

        public ChucNang SelectedFunction
        {
            get { return _selectedFunction; }
            set
            {
                if (_selectedFunction != value)
                {
                    _selectedFunction = value;
                    RaisePropertyChanged("SelectedFunction");
                }
            }
        }

        #region Commands

        public RelayCommand DeleteCommand
        {
            get { return new RelayCommand(() => { }); }
        }

        public RelayCommand ListBoxSelectionCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    if (SelectedFunction != null)
                    {
                        if(User.GetRole == GroupUser.Manager) Messenger.Default.Send(new StatusMessage("Bạn đang đăng nhập với quyền quản lý", 10000, StatusType.Info));
                        else Messenger.Default.Send(new StatusMessage("Bạn đang đăng nhập với quyền Nhân viên vì thế có 1 số chức năng không được thực thi", 5000, StatusType.Warning));
                    }
                });
            }
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(IFunction functionService)
        {
            _functionService = functionService;

            if (SimpleIoc.Default.IsRegistered<NguoiSuDung>())
            {
                User = SimpleIoc.Default.GetInstance<NguoiSuDung>();
            }
#if !DEBUG
            else
            {
                MessageBox.Show("Người dùng không tồn tại");
                Application.Current.Shutdown();
            }
#endif
            Functions = functionService.GetFunctions(User);
            _view = CollectionViewSource.GetDefaultView(Functions);
            _view.GroupDescriptions.Clear();
            _view.GroupDescriptions.Add(new PropertyGroupDescription("Nhom"));
            SimpleIoc.Default.Register<List<ChucNang>>(() => Functions);
        }

        ////public override void Cleanup()
        ////{
        ////    // Clean up if needed

        ////    base.Cleanup();
        ////}


    }
}